package icraf.carbon.gui;

import icraf.carbon.core.AbacusContainer;
import icraf.carbon.core.Project;
import icraf.carbon.gui.MenuFactory.MenuProperty;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

public class InputLabelPanel extends JPanel {
	public interface LabelListener {
		public void labelChanged();
	}
	
	public class Listener implements ActionListener, FocusListener {

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(projectLabel)
					|| e.getSource().equals(projectDesc)) {
				updateText();
			}
		}

		@Override
		public void focusGained(FocusEvent e) {
		}

		@Override
		public void focusLost(FocusEvent e) {
			if (e.getSource().equals(projectLabel)
					|| e.getSource().equals(projectDesc)) {
				updateText();
			}
		}

		private void updateText() {
			project.setLabel(projectLabel.getText());
			project.setDescription(projectDesc.getText());
			if(labelListener != null) labelListener.labelChanged();
		}
	}

	private static final long serialVersionUID = -5844291625397786356L;
	private Project project;
	private Listener listener = new Listener();
	private JTextField projectLabel;
	private JTextArea projectDesc;
	private JPanel mainPanel;
	private LabelListener labelListener;
	
	public InputLabelPanel() {
		super();
		JLabel l1 = new JLabel(MenuProperty.INPUT_LABEL.getLabel() + ":");
		JLabel l2 = new JLabel(MenuProperty.INPUT_DESC.getLabel() + ":");
		l2.setHorizontalAlignment(SwingConstants.LEFT);

		projectLabel = new JTextField(40);
		projectDesc = new JTextArea(6, 40);
		projectDesc.setBorder(projectLabel.getBorder());
		projectDesc.setLineWrap(true);
		projectLabel.addFocusListener(listener);
		projectDesc.addFocusListener(listener);
		projectLabel.addActionListener(listener);
		projectDesc.setFont(projectLabel.getFont());
		
		mainPanel = new JPanel();
		mainPanel.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHWEST;
		c.gridx = 0;
		c.gridy = 0;
		mainPanel.add(l1, c);
		c.gridx = 1;
		mainPanel.add(projectLabel, c);
		c.gridx = 0;
		c.gridy = 1;
		mainPanel.add(l2, c);
		c.gridx = 1;
		mainPanel.add(projectDesc, c);

		//this.setLayout(new BorderLayout());
		this.add(mainPanel, BorderLayout.CENTER);
	}

	public void addMenuPanel(JPanel p){
		GridBagConstraints c = new GridBagConstraints();

		c.insets = new Insets(5, 5, 5, 5);
		c.anchor = GridBagConstraints.NORTHWEST;
		c.gridx = 1;
		c.gridy = 2;
		mainPanel.add(p, c);
	}
	
	public AbacusContainer getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
		projectLabel.setText(project.getLabel());
		projectDesc.setText(project.getDescription());
	}

	public void setLabelListener(LabelListener labelListener) {
		this.labelListener = labelListener;
	}
}
